#include "levelsproxymodel.h"


#include <dtwentymodel.h>


LevelsProxyModel::LevelsProxyModel(QObject *parent) :
    QSortFilterProxyModel(parent)
{
}


QVariant LevelsProxyModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
    {
        return QVariant();
    }

    switch (role)
    {
    case Qt::TextAlignmentRole:
        return alignmentData(index);

    default:
        break;
    }

    return QSortFilterProxyModel::data(index, role);
}

QVariant LevelsProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Vertical)
    {
        if (role == Qt::DisplayRole) return section+1;
        return QVariant();
    }

    switch (section)
    {
    case 0:
        if (role == Qt::DisplayRole) return tr("Level");
        break;

    case 1:
        if (role == Qt::DisplayRole) return tr("Object Name");
        break;

    case 2:
        if (role == Qt::DisplayRole) return tr("Hit Points");
        break;

    case 3:
        if (role == Qt::DisplayRole) return tr("SP");
        break;

    case 4:
        if (role == Qt::DisplayRole) return tr("BA");
        break;

    case 5:
        if (role == Qt::DisplayRole) return tr("Fort");
        break;

    case 6:
        if (role == Qt::DisplayRole) return tr("Ref");
        break;

    case 7:
        if (role == Qt::DisplayRole) return tr("Will");
        break;

    case 8:
        if (role == Qt::DisplayRole) return tr("Special");
        if (role == Qt::TextAlignmentRole) return Qt::AlignLeft;
        break;

    default:
        break;
    }


    return QVariant();
}

Qt::ItemFlags LevelsProxyModel::flags(const QModelIndex &index) const
{
    return QAbstractItemModel::flags(index);
}

#if 0
QModelIndex LevelsProxyModel::classLevelsIndex() const
{
    if (m_characterClass.isNull())
    {
        qDebug() << Q_FUNC_INFO << "Class pointer is null!";
        return QModelIndex();
    }

    int characterClassType = qMetaTypeId<CharacterClass*>();

    QModelIndex start = index(0, 0, QModelIndex());
    QModelIndexList classIndexes = match(start, DTwentyModel::Role_TYPE, characterClassType, -1, Qt::MatchRecursive);
    foreach (QModelIndex classIndex, classIndexes)
    {
        if (classIndex.data().toString() != m_characterClass->name())
        {
            continue;
        }

        QModelIndex levelsIndex = classIndex.child(0,0);
        while (levelsIndex.isValid())
        {
            if (levelsIndex.data().toString().contains("levels", Qt::CaseInsensitive))
            {
                return levelsIndex;
            }
            levelsIndex = levelsIndex.sibling(levelsIndex.row()+1, levelsIndex.column());
        }
    }

    qDebug() << Q_FUNC_INFO << "Failed to find class" << m_characterClass->name() << "levels index by type" << QMetaType::typeName(characterClassType);
    return QModelIndex();
}

void LevelsProxyModel::setCharacterClass(CharacterClass *characterClass)
{
    beginResetModel();

    m_characterClass = characterClass;

    endResetModel();
}
#endif

void LevelsProxyModel::revert()
{
    emit layoutAboutToBeChanged();
    emit layoutChanged();
}


QVariant LevelsProxyModel::alignmentData(const QModelIndex index) const
{
    if (!index.sibling(index.row(), index.column()+1).isValid())
    {
        return QSortFilterProxyModel::data(index);
    }

    return Qt::AlignCenter;
}
